package com.aaa.controller;

import com.aaa.entity.Emp;
import com.aaa.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;


@RestController
@RequestMapping("emp")
@Slf4j
public class EmpController {
    @Resource
    EmpService empService;

    @RequestMapping("list")
    @ResponseBody
    public List<Emp> list(){
        return empService.list();
    }

    @RequestMapping("login")
    public String login(String eusername, String epassword){
        List<Emp> login=empService.login(eusername, epassword);
        if(login.size()==0){
            return "login";
        }else {
            return eusername;
        }

    }

//    @RequestMapping("selectEmp")
//    public List<Emp>select(Emp emp){
//        return empService.select(emp);
//    }
//    //添加
//    @RequestMapping("insert")
//    public int insert(Emp emp){
//        return empService.insert(emp);
//    }
//    //修
//    @RequestMapping("update")
//    public int update(Emp emp){
//        return empService.update(emp);
//    }

    // 根据token判断是否已经认证，认证成功，会自动获取认证信息
    @RequestMapping("getAuthUser")
    public Authentication user(Authentication authentication){
        String name = authentication.getName();
        System.out.println("name>>>>>>"+name);

        // 存的username
        Object principal = authentication.getPrincipal();
        System.out.println(principal.toString());

        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        System.out.println(authentication.toString());

        return authentication;
    }

}
